import logging

import logging
logger = logging.getLogger('peewee')
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.DEBUG)

import sys,os
sys.path.append(os.path.realpath('.'))


from database.model import *


def test_chapter_model():
    print(Chapter.select().count())
    print('test parent chapter')
    parent_chapter=Chapter.get_by_id(11)
    print(parent_chapter.__dict__)
    print('sub chatper count:',len(parent_chapter.chapters))
    print('question count:',parent_chapter.question_count)
    print('test sub chapter')
    sub_chapter=Chapter.get_by_id(1111)
    print(sub_chapter.__dict__)
    print(sub_chapter.parent.__dict__)
    print('question count:',sub_chapter.question_count)

def test_question_model():
    print('test questin model')
    q=Question.get_by_id(12)
    print(q.title)
    question=Question.select()[1]
    print(question.__dict__)
    print('option count:',len(question.options))
    print(question.chapter_id)

def test_option_model():
    print('test option')
    option=Option.select()[0]
    print(option.__dict__)
    print(option.question.__dict__)

def build_answersheet_table():
    try:
        SheetItem.drop_table()
    except:
        print('Answer table not exists.')
    SheetItem.create_table()
    print('Create answer table done!')
    for q in Question.select():
        SheetItem.create(question=q)
    print('Seed answer table done!')

def test_answer_model():
    print('Test answer sheet model...')
    answer=SheetItem.get_by_id(1)
    print(answer.__dict__)
    question=answer.question
    print('question title:',question.title)
    print('question answer:',question.answer_sheet.checked)

if __name__ == '__main__':
    # test_chapter_model()
    test_question_model()
    # test_option_model()
    # build_answer_table()
    # test_answer_model()
    pass